package com.taikang.system.controller.admin;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.taikang.auth.entity.User;
import com.taikang.auth.service.IUserService;
import com.taikang.base.controller.BaseController;
import com.taikang.base.token.TokenValidate;
import com.taikang.base.util.NumberUtil;
import com.taikang.system.entity.SysDict;
import com.taikang.system.form.SysDictForm;
import com.taikang.system.service.ISysDictService;

/**
 * 数据字典管理
 * @creator     taikang
 * @create-time 2016年3月28日   下午2:21:49
 * @email taikang@taikang.com
 * @company www.taikang.com
 * @version 1.0
 */
@Controller("sysDictController")
@Scope("prototype")
@RequestMapping(value = "/admin/system/dict")
public class SysDictController extends BaseController {
	
	@Resource(name = "sysDictServiceImpl")
	private ISysDictService sysDictService;
	@Resource(name = "userServiceImpl")
	private IUserService userService;
	
	/**
	 * 数据库字典首页
	 * 
	 * @return
	 */
	@RequestMapping("/index.htm")
	public ModelAndView index(@ModelAttribute SysDictForm model, HttpServletRequest request, HttpServletResponse response) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/system/sys_dict_list");
		Map<String, Object> data = view.getModel();
		List<SysDict> sysDictList = sysDictService.listAll();
		data.put("sysDictList", sysDictList);
		return view;
	}

	/**
	 * 跳转新增数据页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/edit.htm", method = RequestMethod.GET)
	public ModelAndView edit(@ModelAttribute SysDictForm model,HttpServletRequest request,HttpServletResponse response) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/system/sys_dict_edit");
		Map<String, Object> data = view.getModel();
		
		if(null != model.getItem().getId()){
			SysDict dict = sysDictService.getSysDictById(model.getItem().getId());
			data.put("item", dict);
		}
		
		return view;
	}

	/**
	 * 保存字典
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/saveDict.htm", method = RequestMethod.POST)
	@TokenValidate
	public void saveDict(SysDictForm model, HttpServletRequest request,HttpServletResponse response) {
		try{
			User currentUser = userService.getCurrentUser();
			
			//保存字典
			SysDict item = model.getItem();
			item.setCreateBy(currentUser.getAccount());
			item.setUpdateBy(currentUser.getAccount());
			
			if(null == model.getItem().getId()){
				sysDictService.add(item);
			}else{
				sysDictService.update(item);
			}

			//查询全部
			JSONArray sysDictList = sysDictService.listSysDictJsonAll();
			JSONObject result = new JSONObject();
			result.put("status", "success");
			result.put("options", sysDictList);

			//返回页面
			ajaxPageResult(result, response);
		}catch(Exception ex){
			String msg = "保存字典时发生异常：" + ex.getMessage();
			logger.error(msg,ex);
		}
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/del.htm", method = RequestMethod.POST)
	public void del(SysDictForm model, HttpServletRequest request,HttpServletResponse response) {

		//删除
		sysDictService.delByIds(NumberUtil.convertStringArrayToLongArray(model.getSelIds().split(",")));

		//查询全部
		JSONArray sysDictList = sysDictService.listSysDictJsonAll();
		JSONObject result = new JSONObject();
		result.put("status", "success");
		result.put("options", sysDictList);

		//返回页面
		ajaxPageResult(result, response);
	}
}
